<?php

class PropertyController extends GTL_Action {

    public function init() {
        parent::init();
        $this->lng_id = '1';
        /* Initialize action controller here */
    }

    public function indexAction() {
        $this->_helper->redirector('form', 'property', 'default');
    }

    public function listAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "prp_publish = '1'";
        $orderField = 'prp_id';
        $sort = 'asc';




        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchAllCountry("country_lang_id = '" . $this->lng_id . "'");

        $areaModel = new Model_Area();
        $areaArray = $areaModel->fetchAllArea("area_lang_id = '" . $this->lng_id . "'");



        $enoriaModel = new Model_Enoria();
        $enoriaArray = $enoriaModel->fetchAllEnoria("en_lang_id = '" . $this->lng_id . "'");



        $cityModel = new Model_City();
        $cityArray = $cityModel->fetchAllCity("city_lang_id = '" . $this->lng_id . "'");

        $propertyTypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertyTypeModel->fetchAllPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");


        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;


        $propertyModel = new Model_Property();
        //$propertyData = $propertyModel->fetchProperty($where);
        $propertyData = $propertyModel->fetchPropertyListing($where, $itemsPerPageReviews, $offset, $orderField, $sort);



        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($propertyModel->getPropertyCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'property', 'action' => 'list', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->propertyData = $propertyData;


        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->countryArray = $countryArray;
        $this->view->propertyTypeArray = $propertyTypeArray;
        $this->view->areaArray = $areaArray;
        $this->view->enoriaArray = $enoriaArray;
        $this->view->cityArray = $cityArray;
    }

    public function listedAction() {

        $errorMessage = Array();
        $successmessage = "";
        $where = "prp_publish = '1'";
        $orderField = 'prp_id';
        $sort = 'asc';




        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchAllCountry("country_lang_id = '" . $this->lng_id . "'");

        $areaModel = new Model_Area();
        $areaArray = $areaModel->fetchAllArea("area_lang_id = '" . $this->lng_id . "'");



        $enoriaModel = new Model_Enoria();
        $enoriaArray = $enoriaModel->fetchAllEnoria("en_lang_id = '" . $this->lng_id . "'");



        $cityModel = new Model_City();
        $cityArray = $cityModel->fetchAllCity("city_lang_id = '" . $this->lng_id . "'");

        $propertyTypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertyTypeModel->fetchAllPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");


        /* Pagination Login */
        $itemsPerPageReviews = $this->config->PAGING_PER_PAGE;
        $currentPageReviews = 1;

        //PAGING_PER_PAGE

        if ($this->_getParam('page') && $this->_getParam('page') != "") {
            $currentPageReviews = $this->_getParam('page');
        }

        if ($this->_getParam('field') && $this->_getParam('field') != "") {
            $orderField = $this->_getParam('field');
        }

        if ($this->_getParam('sort') && $this->_getParam('sort') != "") {
            $sort = $this->_getParam('sort');
        }


        $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;


        $propertyModel = new Model_Property();
        //$propertyData = $propertyModel->fetchProperty($where);
        $propertyData = $propertyModel->fetchPropertyListing($where, $itemsPerPageReviews, $offset, $orderField, $sort);



        $paginator = new GTL_Paginator();
        $paginator->setItemsPerPage($itemsPerPageReviews);
        $paginator->setItemsTotal($propertyModel->getPropertyCount($where));
        $paginator->setCurrentPage($currentPageReviews);
        $link = $this->view->url(array('controller' => 'property', 'action' => 'listed', 'page' => 'PAGENO'), 'default', false);
        $paginator->paginate($link);

        $this->view->paginator = $paginator;
        $this->view->propertyData = $propertyData;


        $this->view->errorMessage = $errorMessage;
        $this->view->successmessage = $successmessage;
        $this->view->countryArray = $countryArray;
        $this->view->propertyTypeArray = $propertyTypeArray;
        $this->view->areaArray = $areaArray;
        $this->view->enoriaArray = $enoriaArray;
        $this->view->cityArray = $cityArray;
    }

    public function uploadAction() {

        $upload = new Zend_File_Transfer_Adapter_Http();

        foreach ($upload->getFileInfo() as $fields => $asFileInfo) {

            if ($asFileInfo['name'] != '') {
                switch ($fields) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }

                if ($this->getRequest()->isPost()) {

                    $postedData = $this->_request->getPost();
                    if ($postedData['UNIQUEIDENTIFIER'] != '') {
                        $upload->setDestination($folder);
                        $originalFilename = pathinfo($asFileInfo['name']);
                        $file_name = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                        $upload->addFilter('Rename', $file_name, 'filename');
                        $upload->receive($fields);


                        if ($fields == 'DOCUMENT') {
                            $propertyDocumentModel = new Model_Propertydocument();
                            $propertyDocData = Array();
                            $propertyDocData['prp_id'] = '';
                            $propertyDocData['prpd_doc'] = $file_name;
                            $propertyDocData['prpd_type'] = $fields;
                            $propertyDocData['prpd_identifier'] = $postedData['UNIQUEIDENTIFIER'];
                            $propertyDocData['prpi_createddate'] = date('Y-m-d H:i:s');
                            $propertyDocumentModel->save($propertyDocData, 'prpi_id');
                        } else {

                            $propertyImageModel = new Model_Propertyimage();
                            $propertyImageData = Array();
                            $propertyImageData['prp_id'] = "";
                            $propertyImageData['prpi_img'] = $file_name;
                            $propertyImageData['prpi_type'] = $fields;
                            $propertyImageData['prpi_identifier'] = $postedData['UNIQUEIDENTIFIER'];
                            $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                            $propertyImageModel->save($propertyImageData, 'prpi_id');
                        }
                    }
                }
            }
        }


        exit;
    }

    public function formAction() {

	$this->view->headTitle('LT Admin');
        $uniqueId = uniqid('unq_');

        $successmessage = "";
        $errorMessageArray = Array();
        $propertyKMLUpdateData = Array();
        $this->view->mode = "Add";
        $data = Array();
        $prpi_main_image = "";
        $prpi_fyllo_sxedio_image = "";
        $prp_hdn_subtype = "";

        $areaModel = new Model_Area();
        $enoriaModel = new Model_Enoria();
        $cityModel = new Model_City();

        $propertyLogModel = new Model_Propertylog();

        $memberDetailsModel = new Model_Memberdetails();

        $markerArray = Array();

        $propertySubTypeModel = new Model_Propertysubtype();

        $kmlModel = new Model_Propertykml();
        $propertyImageModel = new Model_Propertyimage();
        $propertyDocumentModel = new Model_Propertydocument();
        $propertykmlcoordinatesModel = new Model_Propertykmlcoordinates();

        $prp_id = "";

        $propertyImages = $propertyImageModel->fetchPropertyImagesByPropertId();
        $propertyDocuments = $propertyDocumentModel->fetchPropertyDocumentsByPropertId();

        $languageModel = new Model_Language();
        $languagesArray = $languageModel->fetchlanguages();
        $languages = Array();
        foreach ($languagesArray as $key => $_lan) {
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']] = $_lan;
            $languages[$_lan['lang_id']]['description'] = "";
        }

        $LATITUDE = $this->view->LATITUDE;
        $LONGITUDE = $this->view->LONGITUDE;

        $MARKERLATITUDE = '';
        $MARKERLONGITUDE = '';

        $memberModel = new Model_Member();
        $propertyForm = new Form_Property();

        $propertyModel = new Model_Property();
        $propertytagModel = new Model_Propertytag();

        $propertytagtypeModel = new Model_Propertytagtype();

        $secondryData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = 'SECONDRY'");
        $indoorData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = 'INDOOR'");
        $outdoorData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = 'OUTDOOR'");
        $tenancyData = $propertytagtypeModel->fetchPropertytagtype("prp_tgtype_lang_id = '" . $this->lng_id . "' and prp_tgtype_type = 'TENANCY'");

        $propertyForm->prp_secondry->addMultiOptions($secondryData);
        $propertyForm->prp_indoor->addMultiOptions($indoorData);
        $propertyForm->prp_outdoor->addMultiOptions($outdoorData);
        $propertyForm->prp_tenancy->addMultiOptions($tenancyData);

        $propertyDescriptionModel = new Model_Propertydescription();


        //Load property select box
        $propertytypeModel = new Model_Propertytype();
        $propertyTypeArray = $propertytypeModel->fetchPropertytype("prtyp_ln_id = '" . $this->lng_id . "'");
        $propertyForm->property_type->addMultiOptions($propertyTypeArray);


        //Load country select box
        $countryModel = new Model_Country();
        $countryArray = $countryModel->fetchCountry("country_lang_id = '" . $this->lng_id . "'");
        $propertyForm->mem_country->addMultiOptions($countryArray);
        $propertyForm->prp_country->addMultiOptions($countryArray);


        $propertyForm->prp_bedrooms->addMultiOptions($this->getbedrooms());
        $propertyForm->prp_bathrooms->addMultiOptions($this->getbathrooms());

        $propertyForm->prp_plotsize->addMultiOptions($this->getplotsize());

        $propertyForm->prp_floor->addMultiOptions($this->getfloor());



        //Check for the customer Parameter
        $propertyData['prp_id'] = $this->_request->getParam('prp_id');
        $prp_id = (!empty($propertyData['prp_id'])) ? $propertyData['prp_id'] : null;




        if (!empty($prp_id)) {

            //Set the Mode to Edit
            $this->view->mode = "Edit";

            //Fetch data to be loaded in form 
            $data = $propertyModel->fetchEntryById($prp_id);

            $propertyForm->member_email->setAttrib('readonly', 'true');

            $prpi_main_image = $data['prp_image'];

            $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $data['mem_country'] . "'");
            $propertyForm->mem_city->addMultiOptions($cityArray);

            $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $data['property_type'] . "'");
            $propertyForm->property_subtype->addMultiOptions($propertySubTypeArray);


            $prpCityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $data['prp_country'] . "'");
            $propertyForm->prp_city->addMultiOptions($prpCityArray);

            $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and city_id = '" . $data['prp_city'] . "'");
            $propertyForm->prp_enoria->addMultiOptions($enoriaArray);

            $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $data['prp_city'] . "'");
            $propertyForm->prp_area->addMultiOptions($areaArray);

            $descriptionData = $propertyDescriptionModel->fetchDescriptionByPropertyId($prp_id);


            foreach ($descriptionData as $_desc) {
                if (array_key_exists($_desc['prp_lng_id'], $languages)) {
                    $languages[$_desc['prp_lng_id']]['description'] = $_desc['prd_description'];
                }
            }


            $tagdataArray = $propertytagModel->fetchTagByPropertyId($prp_id);

            $data['prp_secondry'] = $tagdataArray['SECONDRY'];
            $data['prp_indoor'] = $tagdataArray['INDOOR'];
            $data['prp_outdoor'] = $tagdataArray['OUTDOOR'];
            $data['prp_tenancy'] = $tagdataArray['TENANCY'];

            $propertyImages = $propertyImageModel->fetchPropertyImagesByPropertId($prp_id);

            $propertyDocuments = $propertyDocumentModel->fetchPropertyDocumentsByPropertId($prp_id);

            $markerArray = $propertykmlcoordinatesModel->fetchPropertyKmlCoordinatesByPrpId($prp_id);


            $LATITUDE = $data['prp_lat'];
            $LONGITUDE = $data['prp_lon'];
            $prp_hdn_subtype = $data['property_subtype'];
            
        }

        $this->view->form = $propertyForm->populate($data);

        if ($this->getRequest()->isPost()) {

            //Check for Errors
            $postedData = $this->_request->getPost();
            
            
            $prp_hdn_subtype = $postedData['property_subtype'];
            
            
            if ($postedData['UNIQUEIDENTIFIER'] != '') {
                $uniqueId = $postedData['UNIQUEIDENTIFIER'];
            }
            
            if ($postedData['mem_country'] != '') {
                $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['mem_country'] . "'");
                $propertyForm->mem_city->addMultiOptions($cityArray);
            }

            if ($postedData['prp_country'] != '') {

                $prpCityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['prp_country'] . "'");
                $propertyForm->prp_city->addMultiOptions($prpCityArray);
            }

            if ($postedData['property_type'] != '') {
                $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $postedData['property_type'] . "'");
                $propertyForm->property_subtype->addMultiOptions($propertySubTypeArray);
            }

            if ($postedData['prp_lat'] != '') {
                $LATITUDE = $postedData['prp_lat'];
            }

            if ($postedData['prp_lon'] != '') {
                $LONGITUDE = $postedData['prp_lon'];
            }

            if (array_key_exists('prp_city', $postedData)) {
                if ($postedData['prp_city'] != '') {

                    $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['prp_city'] . "'");
                    $propertyForm->prp_enoria->addMultiOptions($enoriaArray);

                    $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['prp_city'] . "'");
                    $propertyForm->prp_area->addMultiOptions($areaArray);
                }
            }

            if (!$propertyForm->isValid($postedData)) {
                $errorMessage = $propertyForm->getMessages();
                foreach ($errorMessage as $_err) {
                    foreach ($_err as $_val) {
                        $errorMessageArray[] = $_val;
                    }
                }
            } else {

                //KML Validation
                if (array_key_exists('kml', $_FILES)) {
                    if ($_FILES['kml']['name'] != '') {
                        $gtlObj = new GTL_Common();
                        $imageAllowedFormat = $gtlObj->allowedImageFormats();
                        $responseText = $gtlObj->image_validate(Array('application/vnd.google-earth.kml+xml'), 'kml');
                        if ($responseText == 'IMPROPER_FORMAT') {
                            $errorMessageArray[] = 'Please upload KML file.';
                        }
                    }
                }


                //Main Image Validation
                if (array_key_exists('prpi_main_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_main_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload main image with valid file format.';
                    }
                }


                //Fyllo Sxedio
                if (array_key_exists('prpi_fyllo_sxedio', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_fyllo_sxedio');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload Fyllo Sxedio image with valid file format.';
                    }
                }

                //Extra Image Validation
                if (array_key_exists('prpi_extra_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_extra_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload Extra images with valid file format.';
                    }
                }



                //360 Image Validation
                if (array_key_exists('prpi_360_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_360_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload 360 images with valid file format.';
                    }
                }

                //Floor Image Validation
                if (array_key_exists('prpi_floor_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_floor_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload floor images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_bedroom_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_bedroom_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload bedroom images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_living_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_living_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload living images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_bathroom_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_bathroom_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload bathroom images with valid file format.';
                    }
                }

                if (array_key_exists('prpi_outdoor_image', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->image_validate($imageAllowedFormat, 'prpi_outdoor_image');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload outdoor images with valid file format.';
                    }
                }

                //Document Upload Logic
                if (array_key_exists('prpi_document', $_FILES)) {
                    $gtlObj = new GTL_Common();
                    $imageAllowedFormat = $gtlObj->allowedImageFormats();
                    $responseText = $gtlObj->extension_validate(Array('ppt', 'pptx', 'xls', 'xlsx', 'odp', 'doc', 'docx', 'pdf', 'ppsx'), 'prpi_document');
                    if ($responseText == 'IMPROPER_FORMAT') {
                        $errorMessageArray[] = 'Please upload document with valid file format.';
                    }
                }

                if (count($errorMessageArray) > 0) {
                    
                } else {
                    $member_username = $postedData['member_username'];
                    if ($member_username == '') {
                        $usernameArray = explode("@", $postedData['member_email']);
                        $member_username = $usernameArray['0'];
                    }

                    //Check for the email exist while Adding custoemr
                    $memberExist = $memberModel->checkEmailExist($postedData['member_email']);
                    if ($memberExist) {

                        $memberData = Array();
                        $memberData['member_username'] = $member_username;
                        $memberData['member_id'] = $memberExist->member_id;
                        $memberData['member_phone'] = $postedData['member_phone'];
                        $memberModel->save($memberData, 'member_id');

                        $member_id = $memberExist->member_id;
                        $memberDetailsArray['memd_id'] = $memberExist->memd_id;
                        $memberDetailsArray['mem_name'] = $postedData['mem_name'];
                        $memberDetailsArray['mem_address'] = $postedData['mem_address'];
                        $memberDetailsArray['mem_surname'] = $postedData['mem_surname'];
                        $memberDetailsArray['mem_country'] = $postedData['mem_country'];
                        $memberDetailsArray['mem_city'] = $postedData['mem_city'];
                        $memberDetailsArray['mem_mobile'] = $postedData['mem_mobile'];
                        $memberDetailsArray['mem_landline'] = $postedData['mem_landline'];
                        $memberDetailsArray['mem_work'] = $postedData['mem_work'];
                        $memberDetailsArray['mem_representative_name'] = $postedData['mem_representative_name'];
                        $memberDetailsArray['mem_representative_telephone1'] = $postedData['mem_representative_telephone1'];
                        $memberDetailsArray['mem_representative_telephone2'] = $postedData['mem_representative_telephone2'];
                        $memberDetailsArray['mem_representative_telephone3'] = $postedData['mem_representative_telephone3'];
                        $memberDetailsArray['member_id'] = $member_id;
                        $memberDetailsModel->save($memberDetailsArray, 'memd_id');
                    } else {
                        $memberData = Array();
                        $generatedPasswrod = GTL_Common::generatePassword();
                        $memberData['member_username'] = $member_username;
                        $memberData['member_email'] = $postedData['member_email'];
                        $memberData['member_password'] = md5($generatedPasswrod);
                        $memberData['member_admin'] = 0;
                        $memberData['member_phone'] = $postedData['member_phone'];
                        $memberData['member_forgot'] = "";
                        $memberData['oauth_provider'] = "0";
                        $memberData['oauth_access_token'] = "";
                        $memberData['oauth_user_key'] = "";
                        $memberData['member_admin_level'] = "1";

                        $member_id = $memberModel->save($memberData, 'member_id');

                        if ($member_id > 0) {

                            $memberDetailsArray = Array();
                            $memberDetailsArray['mem_title'] = "";
                            $memberDetailsArray['mem_name'] = $postedData['mem_name'];
                            $memberDetailsArray['mem_address'] = $postedData['mem_address'];
                            $memberDetailsArray['mem_surname'] = $postedData['mem_surname'];
                            $memberDetailsArray['mem_country'] = $postedData['mem_country'];
                            $memberDetailsArray['mem_city'] = $postedData['mem_city'];


                            $memberDetailsArray['mem_mobile'] = $postedData['mem_mobile'];
                            $memberDetailsArray['mem_landline'] = $postedData['mem_landline'];
                            $memberDetailsArray['mem_work'] = $postedData['mem_work'];
                            $memberDetailsArray['mem_representative_name'] = $postedData['mem_representative_name'];
                            $memberDetailsArray['mem_representative_telephone1'] = $postedData['mem_representative_telephone1'];
                            $memberDetailsArray['mem_representative_telephone2'] = $postedData['mem_representative_telephone2'];
                            $memberDetailsArray['mem_representative_telephone3'] = $postedData['mem_representative_telephone3'];

                            $memberDetailsArray['mem_bday'] = date('Y-m-d');
                            $memberDetailsArray['member_id'] = $member_id;
                            $memberDetailsModel->save($memberDetailsArray, 'memd_id');
                        }
                    }

                    //echo $member_id;
                    $propertyData = Array();
                    $propertyData['prp_id'] = $prp_id;
                    $propertyData['member_id'] = $member_id;
                    $propertyData['property_type'] = $postedData['property_type'];
                    $propertyData['property_subtype'] = $postedData['property_subtype'];
                    $propertyData['prp_displayaddress'] = $postedData['prp_displayaddress'];
                    $propertyData['prp_address'] = $postedData['prp_address'];
                    $propertyData['prp_lat'] = $postedData['prp_lat'];
                    $propertyData['prp_lon'] = $postedData['prp_lon'];
                    $propertyData['prp_country'] = $postedData['prp_country'];
                    $propertyData['prp_city'] = $postedData['prp_city'];
                    $propertyData['prp_area'] = $postedData['prp_area'];
                    $propertyData['prp_enoria'] = $postedData['prp_enoria'];

                    $propertyData['prp_bedrooms'] = $postedData['prp_bedrooms'];
                    $propertyData['prp_bathrooms'] = $postedData['prp_bathrooms'];
                    $propertyData['prp_saleprice'] = $postedData['prp_saleprice'];
                    $propertyData['prp_rentprice'] = $postedData['prp_rentprice'];
                    $propertyData['prp_plotsize'] = $postedData['prp_plotsize'];
                    $propertyData['prp_plotarea'] = $postedData['prp_plotarea'];
                    $propertyData['prp_built_year'] = $postedData['prp_built_year'];
                    $propertyData['prp_coveredarea'] = $postedData['prp_coveredarea'];
                    $propertyData['prp_coveredverenda'] = $postedData['prp_coveredverenda'];
                    $propertyData['prp_floor'] = $postedData['prp_floor'];
                    $propertyData['prp_entrynumber'] = $postedData['prp_entrynumber'];
                    $propertyData['prp_plotnumber'] = $postedData['prp_plotnumber'];
                    $propertyData['prp_zone'] = $postedData['prp_zone'];
                    $propertyData['prp_frontage'] = $postedData['prp_frontage'];
                    $propertyData['prp_videotext'] = $postedData['prp_videotext'];
                    $propertyData['prp_image'] = $prpi_main_image;
                    $propertyData['prp_publish'] = '1';

                    $prp_id = $propertyModel->save($propertyData, 'prp_id');

                    if ($prp_id > 0) {
                        $upload = new Zend_File_Transfer_Adapter_Http();
                        foreach ($upload->getFileInfo() as $fields => $asFileInfo) {

                            if ($asFileInfo['name'] != '') {
                                //Main Image
                                $is_prpi_main_image_present = strpos($fields, "prpi_main_image");
                                if ($is_prpi_main_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);

                                    $prpi_main_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_main_image, 'filename');
                                    $upload->receive($fields);

                                    $postedData['prp_image'] = $prpi_main_image;

                                    $propertyImageUpdateData = Array();
                                    $propertyImageUpdateData['prp_id'] = $prp_id;
                                    $propertyImageUpdateData['prp_image'] = $prpi_main_image;
                                    $propertyModel->save($propertyImageUpdateData, 'prp_id');

                                    if ($this->view->mode == "Edit") {
                                        $main_imagepath = $folder . $data['prp_image'];
                                        if (file_exists($main_imagepath)) {
                                            unlink($main_imagepath);
                                        }
                                    }
                                }

                                //Fyllo Sxedio
                                $is_prpi_fyllo_sxedio_present = strpos($fields, "prpi_fyllo_sxedio");
                                if ($is_prpi_fyllo_sxedio_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_fyllo_sxedio = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_fyllo_sxedio, 'filename');
                                    $upload->receive($fields);

                                    $FYLLO_SXEDIOArray = $propertyImageModel->fetchPropertyImages("prp_id = '" . $prp_id . "' and prpi_type = 'FYLLO_SXEDIO'");
                                    $propertyImageModel->delete("prp_id = '" . $prp_id . "' and prpi_type = 'FYLLO_SXEDIO'");
                                    foreach ($FYLLO_SXEDIOArray as $_FYLLO_SXEDIO) {
                                        $FYLLO_SXEDIOPath = $folder . $_FYLLO_SXEDIO['prpi_img'];
                                        if (file_exists($FYLLO_SXEDIOPath)) {
                                            unlink($FYLLO_SXEDIOPath);
                                        }
                                    }

                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_fyllo_sxedio;
                                    $propertyImageData['prpi_type'] = 'FYLLO_SXEDIO';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //Extra Images Code
                                $is_prpi_extra_image_present = strpos($fields, "prpi_extra_image");
                                if ($is_prpi_extra_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_extra_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_extra_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_extra_image;
                                    $propertyImageData['prpi_type'] = 'EXTRA_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //360 Images Code
                                $is_360_extra_image_present = strpos($fields, "prpi_360_image");
                                if ($is_360_extra_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_360_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_360_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_360_image;
                                    $propertyImageData['prpi_type'] = 'THREE_SIXTY_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //FLOOR Images Code
                                $is_floor_image_present = strpos($fields, "prpi_floor_image");
                                if ($is_floor_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/floor/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_floor_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_floor_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_floor_image;
                                    $propertyImageData['prpi_type'] = 'FLOOR_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }


                                // BED ROOM IMAGES
                                $is_bedroom_image_present = strpos($fields, "prpi_bedroom_image");
                                if ($is_bedroom_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_bedroom_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_bedroom_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_bedroom_image;
                                    $propertyImageData['prpi_type'] = 'BEDROOM_IMAGE';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //LIVING ROOM  
                                $is_living_image_present = strpos($fields, "prpi_living_image");
                                if ($is_living_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/living/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_living_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_living_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_living_image;
                                    $propertyImageData['prpi_type'] = 'LIVING_ROOM';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                //BATHROOM    
                                $is_bathroom_image_present = strpos($fields, "prpi_bathroom_image");
                                if ($is_bathroom_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_bathroom_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_bathroom_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_bathroom_image;
                                    $propertyImageData['prpi_type'] = 'BATHROOM';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }

                                // Outdoor images 
                                $is_outdoor_image_present = strpos($fields, "prpi_outdoor_image");
                                if ($is_outdoor_image_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_outdoor_image = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_outdoor_image, 'filename');
                                    $upload->receive($fields);
                                    $propertyImageData = Array();
                                    $propertyImageData['prp_id'] = $prp_id;
                                    $propertyImageData['prpi_img'] = $prpi_outdoor_image;
                                    $propertyImageData['prpi_type'] = 'OUTDOOR';
                                    $propertyImageData['prpi_identifier'] = '';
                                    $propertyImageData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyImageModel->save($propertyImageData, 'prpi_id');
                                }



                                // Documents
                                $is_documents_present = strpos($fields, "prpi_document");
                                if ($is_documents_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/document/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);
                                    $prpi_document = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $prpi_document, 'filename');
                                    $upload->receive($fields);
                                    $propertyDocData = Array();
                                    $propertyDocData['prp_id'] = $prp_id;
                                    $propertyDocData['prpd_doc'] = $prpi_document;
                                    $propertyDocData['prpd_type'] = 'DOCUMENT';
                                    $propertyDocData['prpd_identifier'] = '';
                                    $propertyDocData['prpi_createddate'] = date('Y-m-d H:i:s');
                                    $propertyDocumentModel->save($propertyDocData, 'prpi_id');
                                }

                                // BED ROOM IMAGES
                                $is_kml_present = strpos($fields, "kml");
                                if ($is_kml_present === false) {
                                    
                                } else {
                                    $folder = $this->config->UPLOAD_PATH . "property/kml/";
                                    $upload->setDestination($folder);
                                    $originalFilename = pathinfo($asFileInfo['name']);

                                    $kml = preg_replace("/[^-_a-zA-Z0-9]+/i", "_", $originalFilename['filename']) . '_' . time() . '.' . $originalFilename['extension'];
                                    $upload->addFilter('Rename', $kml, 'filename');
                                    $upload->receive($fields);

                                    $target_path = $folder . $kml;

                                    if (file_exists($target_path)) {


                                        if ($this->view->mode == "Edit") {
                                            //Delete Logic of KML file and KML Co-ordinates
                                            $kmlFiles = $kmlModel->fetchPropertyKml("prp_id = '" . $prp_id . "'");
                                            foreach ($kmlFiles as $_kml) {
                                                $folder = $this->config->UPLOAD_PATH . "property/kml/";
                                                $kmlTargetPath = $folder . $_kml['prpk_file'];
                                                if (file_exists($kmlTargetPath)) {
                                                    unlink($kmlTargetPath);
                                                }
                                            }
                                            $kmlModel->delete("prp_id = '" . $prp_id . "'");
                                            $propertykmlcoordinatesModel->delete("prp_id = '" . $prp_id . "'");
                                        }


                                        $kmlData = Array();
                                        $kmlData['prp_id'] = $prp_id;
                                        $kmlData['prpk_file'] = $kml;
                                        $kmlData['prpk_createddate'] = date('Y-m-d H:i:s');
                                        $prpk_id = $kmlModel->save($kmlData, 'prpk_id');
                                        $coordinatesArray = GTL_Common::parseKMl($target_path);

                                        if (count($coordinatesArray) > 0) {
                                            foreach ($coordinatesArray as $key => $_coordiante) {

                                                if ($key == 0) {
                                                    $propertyKMlUpdateData['prp_id'] = $prp_id;
                                                    $propertyKMlUpdateData['prp_lat'] = $_coordiante['lat'];
                                                    $propertyKMlUpdateData['prp_lon'] = $_coordiante['lon'];
                                                }

                                                $propertykmlcoordinatesArray = Array();
                                                $propertykmlcoordinatesArray['prpkc_id'] = NULL;
                                                $propertykmlcoordinatesArray['prpk_id'] = $prpk_id;
                                                $propertykmlcoordinatesArray['prp_id'] = $prp_id;
                                                $propertykmlcoordinatesArray['prpkc_lat'] = $_coordiante['lat'];
                                                $propertykmlcoordinatesArray['prpkc_lon'] = $_coordiante['lon'];
                                                $propertykmlcoordinatesArray['prpkc_createddate'] = date('Y-m-d H:i:s');
                                                $propertykmlcoordinatesModel->save($propertykmlcoordinatesArray, 'prpkc_id');
                                            }
                                        }
                                    }
                                }
                            }
                        }


                        $propertyDescriptionModel->delete("prp_id = '" . $prp_id . "'");

                        /*
                          echo "<pre>";
                          print_R($languages);
                          print_R($postedData['prd_description']);
                          exit;
                         */
                        $i = 0;
                        foreach ($languages as $key => $_lang) {
                            if ($postedData['prd_description'][$i] != '') {
                                $propertyDescriptionData = Array();
                                $propertyDescriptionData['prp_id'] = $prp_id;
                                $propertyDescriptionData['prd_description'] = $postedData['prd_description'][$i];
                                $propertyDescriptionData['prp_lng_id'] = $_lang['lang_id'];
                                $propertyDescriptionModel->save($propertyDescriptionData, 'prd_id');
                            }
                            $i++;
                        }

                        $propertytagModel->delete("prp_id = '" . $prp_id . "'");
                        if (array_key_exists('prp_secondry', $postedData)) {
                            foreach ($postedData['prp_secondry'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = 'SECONDRY';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        if (array_key_exists('prp_indoor', $postedData)) {
                            foreach ($postedData['prp_indoor'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = 'INDOOR';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        if (array_key_exists('prp_outdoor', $postedData)) {
                            foreach ($postedData['prp_outdoor'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = 'OUTDOOR';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }

                        if (array_key_exists('prp_tenancy', $postedData)) {
                            foreach ($postedData['prp_tenancy'] as $_val) {
                                $propertyTagArray['prp_id'] = $prp_id;
                                $propertyTagArray['prp_ln_id'] = $this->lng_id;
                                $propertyTagArray['prp_tag'] = $_val;
                                $propertyTagArray['prp_type'] = 'TENANCY';
                                $propertytagModel->save($propertyTagArray, 'prpt_id');
                            }
                        }
                    }
                    
                    
                    if (count($propertyKMlUpdateData) > 0) {
                        $propertyModel->save($propertyKMlUpdateData, 'prp_id');
                    }

                    //Property Log Entry
                    $propertyLogArray = Array();
                    $propertyLogArray['prplg_id'] = NULL;
                    $propertyLogArray['prp_id'] = $prp_id;
                    $propertyLogArray['member_id'] = $this->view->user->member_id;
                    $propertyLogArray['prp_date'] = date('Y-m-d H:i:s');
                    $propertyLogModel->save($propertyLogArray, "prplg_id");
                    
                    $propertyImageModel->update(Array('prp_id' => $prp_id), "prpi_identifier = '".$uniqueId."'");
                    $propertyDocumentModel->update(Array('prp_id' => $prp_id), "prpd_identifier = '".$uniqueId."'");
                    
                    if ($this->view->mode == "Add") {
                        $successmessage = "Property added successfully";
                    } else {
                        $successmessage = "Property updated successfully";
                    }

                    $this->addSuccessMessage($successmessage);
                    $this->_helper->redirector('list', 'property', '');
                }
            }
        }

        
        
        $this->view->prp_hdn_subtype = $prp_hdn_subtype;
        $this->view->prp_id = $prp_id;
        $this->view->propertyImages = $propertyImages;
        $this->view->propertyDocuments = $propertyDocuments;

        $this->view->prpi_main_image = $prpi_main_image;
        $this->view->languages = $languages;
        $this->view->MARKERLONGITUDE = $MARKERLONGITUDE;
        $this->view->MARKERLATITUDE = $MARKERLATITUDE;


        if (count($markerArray) > 0) {
            $this->view->iskmlpresent = "YES";
        } else {
            $this->view->iskmlpresent = "NO";
        }


        $this->view->UNIQUEIDENTIFIER = $uniqueId;
        $this->view->markerArray = json_encode($markerArray);
        $this->view->LATITUDE = $LATITUDE;
        $this->view->LONGITUDE = $LONGITUDE;
        $this->view->errorMessageArray = $errorMessageArray;
        $this->view->succesMessage = $successmessage;
        $this->view->form = $propertyForm;
    }

    public function getbedrooms() {
        $bedroomArray[] = "-- Select bedrooms --";
        foreach (range(1, 10) as $number) {
            $bedroomArray[$number] = $number;
        }
        return $bedroomArray;
    }

    public function getbathrooms() {
        $bathroomArray[] = "-- Select bathrooms --";
        foreach (range(1, 10) as $number) {
            $bathroomArray[$number] = $number;
        }
        return $bathroomArray;
    }

    public function getplotsize() {
        $plotArray[] = "-- Select plot size --";
        foreach (range(1, 10) as $number) {
            $plotArray[$number] = $number;
        }
        return $plotArray;
    }

    public function getfloor() {
        $floorArray[] = "-- Select floor --";
        foreach (range(1, 10) as $number) {
            $floorArray[$number] = $number;
        }
        return $floorArray;
    }

    public function getcitylistAction() {
        $cityString = "<option>-- Select City--</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['country_id'] != '') {

                $cityModel = new Model_City();
                $cityArray = $cityModel->fetchCity("city_lang_id = '" . $this->lng_id . "' and country_id = '" . $postedData['country_id'] . "'");

                if (count($cityArray) > 0) {
                    $cityString = "";
                    foreach ($cityArray as $city_id => $_city_title) {
                        $cityString .= "<option value='" . $city_id . "'>" . $_city_title . "</option>";
                    }
                }
            }
        }
        echo $cityString;
        exit;
    }

    public function getpropertysublistAction() {
        $propertySubString = "<option>-- Select Property subtype --</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['property_type'] != '') {
                $propertySubTypeModel = new Model_Propertysubtype();
                $propertySubTypeArray = $propertySubTypeModel->fetchPropertySubType("prstyp_ln_id = '" . $this->lng_id . "' and prtyp_id = '" . $postedData['property_type'] . "'");
                if (count($propertySubTypeArray) > 0) {
                    $propertySubString = "";
                    foreach ($propertySubTypeArray as $_id => $_title) {
                        $propertySubString .= "<option value='" . $_id . "'>" . $_title . "</option>";
                    }
                }
            }
        }
        echo $propertySubString;
        exit;
    }

    public function getarealistAction() {
        $areaString = "<option>-- Select Area--</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['city_id'] != '') {
                $areaModel = new Model_Area();
                $areaArray = $areaModel->fetchArea("area_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['city_id'] . "'");
                if (count($areaArray) > 0) {
                    $areaString = "";
                    foreach ($areaArray as $area_id => $_area_title) {
                        $areaString .= "<option value='" . $area_id . "'>" . $_area_title . "</option>";
                    }
                }
            }
        }
        echo $areaString;
        exit;
    }

    public function getenorialistAction() {
        $enoriaString = "<option>-- Select Enoria --</option>";
        if ($this->getRequest()->isPost()) {
            $postedData = $this->_request->getPost();
            if ($postedData['city_id'] != '') {
                $enoriaModel = new Model_Enoria();
                $enoriaArray = $enoriaModel->fetchEnoria("en_lang_id = '" . $this->lng_id . "' and city_id = '" . $postedData['city_id'] . "'");
                if (count($enoriaArray) > 0) {
                    $enoriaString = "";
                    foreach ($enoriaArray as $en_id => $_en_title) {
                        $enoriaString .= "<option value='" . $en_id . "'>" . $_en_title . "</option>";
                    }
                }
            }
        }
        echo $enoriaString;
        exit;
    }

    public function archiveAction() {

        $propertyModel = new Model_Property();


        if ($this->_getParam('prp_id') && $this->_getParam('prp_id') != "") {
            $prp_id = $this->_getParam('prp_id');
            $propertyData = Array();
            $propertyData['prp_id'] = $prp_id;
            $propertyData['prp_publish'] = '0';

            $propertyModel->save($propertyData, 'prp_id');

            $successmessage = "Property archived successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'property', '');
            exit;
        }
    }

    public function deleteAction() {


        if ($this->_getParam('prp_id') && $this->_getParam('prp_id') != "") {
            $prp_id = $this->_getParam('prp_id');

            $propertyModel = new Model_Property();
            $kmlModel = new Model_Propertykml();
            $propertykmlcoordinatesModel = new Model_Propertykmlcoordinates();
            $propertyDescriptionModel = new Model_Propertydescription();
            $propertytagModel = new Model_Propertytag();
            $propertyImageModel = new Model_Propertyimage();
            $propertyDocumentModel = new Model_Propertydocument();

            $propertyDescriptionModel->delete("prp_id = '" . $prp_id . "'");

            $propertytagModel->delete("prp_id = '" . $prp_id . "'");



            $propertyImages = $propertyImageModel->fetchPropertyImages("prp_id = '" . $prp_id . "'");
            foreach ($propertyImages as $_images) {


                switch ($_images['prpi_type']) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }
                $imagePath = $folder . $_images['prpi_img'];
                if (file_exists($imagePath)) {
                    unlink($imagePath);
                }
            }
            $propertyImageModel->delete("prp_id = '" . $prp_id . "'");

            $propertyDocuments = $propertyDocumentModel->fetchPropertyDocuments("prp_id = '" . $prp_id . "'");
            foreach ($propertyDocuments as $_doc) {
                $folder = $this->config->UPLOAD_PATH . "property/document/";
                $docPath = $folder . $_doc['prpd_doc'];
                if (file_exists($docPath)) {
                    unlink($docPath);
                }
            }
            $propertyDocumentModel->delete("prp_id = '" . $prp_id . "'");

            $propertyKmls = $kmlModel->fetchPropertyKml("prp_id = '" . $prp_id . "'");
            foreach ($propertyKmls as $_kml) {
                $folder = $this->config->UPLOAD_PATH . "property/kml/";
                $kmlPath = $folder . $_kml['prpk_file'];
                if (file_exists($kmlPath)) {
                    unlink($kmlPath);
                }
            }
            $kmlModel->delete("prp_id = '" . $prp_id . "'");
            $propertykmlcoordinatesModel->delete("prp_id = '" . $prp_id . "'");

            $data = $propertyModel->fetchEntryById($prp_id);
            $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
            $main_imagepath = $folder . $data['prp_image'];
            if (file_exists($main_imagepath)) {
                unlink($main_imagepath);
            }
            $propertyModel->delete("prp_id = '" . $prp_id . "'");

            $successmessage = "Property deleted successfully";
            $this->addSuccessMessage($successmessage);
            $this->_helper->redirector('list', 'property', '');
            exit;
        }
    }

    public function deleteimageAction() {

        $propertyModel = new Model_Property();
        $propertyImageModel = new Model_Propertyimage();

        $type = "";
        if ($this->_getParam('type') && $this->_getParam('type') != "") {
            $type = $this->_getParam('type');
        }

        if ($type == '') {
            $this->_helper->redirector('list', 'property', '');
            exit;
        }

        if ($this->_getParam('id') && $this->_getParam('id') != "") {
            $id = $this->_getParam('id');
        }

        if ($type == 'MAIN_IMAGE') {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $propertyImageUpdateData = Array();
                $propertyImageUpdateData['prp_id'] = $id;
                $propertyImageUpdateData['prp_image'] = "";
                $propertyData = $propertyModel->fetchEntryById($id);
                $prp_image = $propertyData['prp_image'];
                $folder = $this->config->UPLOAD_PATH . "property/mainimage/";
                $targetPath = $folder . $prp_image;
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $propertyModel->save($propertyImageUpdateData, 'prp_id');
                $successmessage = "Property image deleted successfully";
                $this->addSuccessMessage($successmessage);
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        } else if ($type == 'DOCUMENT') {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $id = $this->_getParam('id');

                $propertyDocumentModel = new Model_Propertydocument();

                $documents = $propertyDocumentModel->fetchEntryById($id);
                $propertyDocumentModel->delete("prpd_id = '" . $id . "'");
                $folder = $this->config->UPLOAD_PATH . "property/document/";
                $targetPath = $folder . $documents['prpd_doc'];
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $successmessage = "Property document deleted successfully";
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        } else {
            if ($this->_getParam('id') && $this->_getParam('id') != "") {
                $id = $this->_getParam('id');
                $propertyImage = $propertyImageModel->fetchEntryById($id);
                $type = $propertyImage['prpi_type'];
                switch ($type) {
                    case "FYLLO_SXEDIO":
                        $folder = $this->config->UPLOAD_PATH . "property/fyllo_sxedio/";
                        break;
                    case "EXTRA_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/extraimages/";
                        break;
                    case "THREE_SIXTY_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/threesixtyimages/";
                        break;
                    case "FLOOR_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/floor/";
                        break;
                    case "BEDROOM_IMAGE":
                        $folder = $this->config->UPLOAD_PATH . "property/bedroom/";
                        break;
                    case "LIVING_ROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/living/";
                        break;
                    case "BATHROOM":
                        $folder = $this->config->UPLOAD_PATH . "property/bathroom/";
                        break;
                    case "OUTDOOR":
                        $folder = $this->config->UPLOAD_PATH . "property/outdoor/";
                        break;
                    case "DOCUMENT":
                        $folder = $this->config->UPLOAD_PATH . "property/document/";
                        break;
                }
                $prp_image = $propertyImage['prpi_img'];
                $targetPath = $folder . $prp_image;
                if (file_exists($targetPath)) {
                    unlink($targetPath);
                }
                $propertyImageModel->delete("prpi_id = '" . $id . "'");
                $successmessage = "Property image deleted successfully";

                $this->addSuccessMessage($successmessage);
                $this->_helper->redirector('list', 'property', '');
                exit;
            }
        }
    }

}

?>